package com.example.cathouse;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.cathouse.databinding.FragmentModifyBinding;


public class ModifyFragment extends Fragment {
    String cat_condition, cat_appearance, cat_relationship, cat_character, cat_sterilization;
    FragmentModifyBinding binding;
    private CatViewModel catViewModel;
    private LiveData<Cat> cat;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        catViewModel = new ViewModelProvider(requireActivity()).get(CatViewModel.class);

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_modify, container, false);
        binding.setData(catViewModel);
        binding.buttonSubmit.setEnabled(false);

        //进入修改界面自动弹出键盘
        binding.editTextCondition.requestFocus();
        InputMethodManager imm = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(binding.editTextCondition, 0);

        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                cat_condition = binding.editTextCondition.getText().toString().trim();
                cat_appearance = binding.editTextAppearance.getText().toString().trim();
                cat_sterilization = binding.editTextSterilization.getText().toString().trim();
                cat_character = binding.editTextCharacter.getText().toString().trim();
                cat_relationship = binding.editTextRelationship.getText().toString().trim();
                binding.buttonSubmit.setEnabled(!cat_appearance.isEmpty() || !cat_character.isEmpty() || !cat_condition.isEmpty() || !cat_sterilization.isEmpty() || !cat_relationship.isEmpty());
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        binding.editTextCondition.addTextChangedListener(textWatcher);
        binding.editTextAppearance.addTextChangedListener(textWatcher);
        binding.editTextSterilization.addTextChangedListener(textWatcher);
        binding.editTextCharacter.addTextChangedListener(textWatcher);
        binding.editTextRelationship.addTextChangedListener(textWatcher);


        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        cat = catViewModel.getCat(getArguments().getInt("cat_id"));
        cat.removeObservers(requireActivity());
        cat.observe(requireActivity(), new Observer<Cat>() {
            @Override
            public void onChanged(Cat cat) {
                if (cat != null) {
                    binding.textViewName.setText(cat.getCatName());
                    binding.buttonSubmit.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            if (cat_appearance.isEmpty()) {
                                cat_appearance = cat.getCatAppearance();
                            }
                            if (cat_sterilization.isEmpty()) {
                                cat_sterilization = cat.getCatSterilization();
                            }
                            if (cat_character.isEmpty()) {
                                cat_character = cat.getCatCharacter();
                            }
                            if (cat_condition.isEmpty()) {
                                cat_condition = cat.getCatCondition();
                            }
                            if (cat_relationship.isEmpty()) {
                                cat_relationship = cat.getCatRelationship();
                            }

                            Cat update_cat = new Cat(cat.getCatName(), cat.getCatColor(), cat.getCatSex(), cat_condition, cat_sterilization, cat_character, cat_appearance, cat_relationship);
                            update_cat.setId(cat.getId());
                            catViewModel.updateCats(update_cat);

                            NavController controller = Navigation.findNavController(v);
                            controller.navigateUp();
                        }
                    });
                }
            }
        });


    }
}